<html>
<head><meta charset="utf-8"><title>how vtable is constructed and organized? · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/how.20vtable.20is.20constructed.20and.20organized.3F.html">how vtable is constructed and organized?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="205963403"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/how%20vtable%20is%20constructed%20and%20organized%3F/near/205963403" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jason Hu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/how.20vtable.20is.20constructed.20and.20organized.3F.html#205963403">(Aug 04 2020 at 21:31)</a>:</h4>
<p>Hi,</p>
<p>I am recently thinking to look into trait objects and it is unavoidable to consider vtables. how in general a vtable is organized? how a vtable is constructed?</p>



<a name="205963736"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/how%20vtable%20is%20constructed%20and%20organized%3F/near/205963736" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/how.20vtable.20is.20constructed.20and.20organized.3F.html#205963736">(Aug 04 2020 at 21:34)</a>:</h4>
<p>Copying an old comment I wrote up:</p>
<p>The <a href="https://github.com/rust-lang/rust/blob/d8cf749570c87a4dcf05071bb5b280febb7f6657/src/librustc_trait_selection/traits/mod.rs#L468"><code>vtable_methods</code></a> query yields all method entries for a specific vtable (that is, the list of all dyn-compatible methods of all transitive supertraits). On <a href="https://github.com/rust-lang/rust/blob/d8cf749570c87a4dcf05071bb5b280febb7f6657/src/librustc_mir/monomorphize/collector.rs#L540-L562">every unsizing cast</a> to a trait object these methods are enqueued for code generation (and the drop glue is also added).</p>
<p>Codegen then <a href="https://github.com/rust-lang/rust/blob/d8cf749570c87a4dcf05071bb5b280febb7f6657/src/librustc_codegen_ssa/meth.rs#L108-L116">assembles the vtable</a> from drop glue, size, alignment, and the method pointers. This defines the vtable's layout in memory.</p>



<a name="205963765"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/how%20vtable%20is%20constructed%20and%20organized%3F/near/205963765" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/how.20vtable.20is.20constructed.20and.20organized.3F.html#205963765">(Aug 04 2020 at 21:35)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/blob/master/src/librustc_codegen_ssa/meth.rs">https://github.com/rust-lang/rust/blob/master/src/librustc_codegen_ssa/meth.rs</a></p>
<p>drop_in_place, size, align, function pointers for all methods (for non object-safe methods a null pointer gets written)</p>



<a name="205964100"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/how%20vtable%20is%20constructed%20and%20organized%3F/near/205964100" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jason Hu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/how.20vtable.20is.20constructed.20and.20organized.3F.html#205964100">(Aug 04 2020 at 21:37)</a>:</h4>
<p>that is brilliant!  thx for the info.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>